package zhang.jdbc.graph;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import zhang.utils.JDBCUtils;
import zhang.utils.User;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class Regisn extends JFrame {
	public static int STUDENT=1;
	public static int TEACHER=0;
	private JPanel contentPane;
	private JTextField idt;
	private JPasswordField passwordt;
	private JPasswordField passwordage;
	private JTextField namet;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Regisn frame = new Regisn();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Regisn() {
		setDefaultCloseOperation(JFrame.TEXT_CURSOR);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("账户：");
		lblNewLabel.setBounds(49, 53, 58, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("密码：");
		lblNewLabel_1.setBounds(49, 105, 58, 15);
		contentPane.add(lblNewLabel_1);
		
		idt = new JTextField();
		idt.setBounds(119, 50, 138, 21);
		contentPane.add(idt);
		idt.setColumns(10);
		
		JRadioButton trad = new JRadioButton("教师");
		JRadioButton srad = new JRadioButton("学生");
		trad.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				srad.setSelected(false);
			}
		});
		
		srad.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				trad.setSelected(false);
			}
		});
		
		trad.setBounds(127, 183, 64, 23);
		contentPane.add(trad);
		
		
		srad.setBounds(193, 183, 64, 23);
		contentPane.add(srad);
		
		JLabel lblNewLabel_2 = new JLabel("选择角色：");
		lblNewLabel_2.setBounds(49, 187, 72, 15);
		contentPane.add(lblNewLabel_2);
		
		JButton submit = new JButton("注册");
		
		submit.setBounds(119, 230, 97, 23);
		contentPane.add(submit);
		
		passwordt = new JPasswordField();
		passwordt.setBounds(117, 102, 140, 21);
		contentPane.add(passwordt);
		
		JLabel qqq = new JLabel("再次输入密码：");
		qqq.setBounds(10, 141, 97, 36);
		contentPane.add(qqq);
		
		passwordage = new JPasswordField();
		passwordage.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				if(passwordt.getText().equals(passwordage.getText())) {
					submit.setEnabled(true);
				}else {
					JOptionPane.showMessageDialog(null, "两次输入的密码不一致！");
					submit.setEnabled(false);
				}
			}
		});
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JDBCUtils jdb = new JDBCUtils();
				User user = new User(idt.getText(),passwordt.getText(),0,namet.getText());
				if(trad.isSelected()==true) {
					user.setRole(0);
				}else {
					user.setRole(1);
				}
				jdb.insert(user);
			}
		});
		passwordage.setBounds(119, 149, 138, 21);
		contentPane.add(passwordage);
		
		JLabel lblNewLabel_3 = new JLabel("姓名：");
		lblNewLabel_3.setBounds(49, 23, 58, 15);
		contentPane.add(lblNewLabel_3);
		
		namet = new JTextField();
		namet.setBounds(119, 19, 140, 21);
		contentPane.add(namet);
		namet.setColumns(10);
		this.setVisible(true);
	}
}
